package com.renwei.yunlong.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.fingdo.statelayout.StateLayout;
import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.adapter.WorkHistoryPagerAdapter;
import com.renwei.yunlong.base.BaseLazyFragment;
import com.renwei.yunlong.bean.ComplainResultBean;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.LogUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;

import java.util.HashMap;

import butterknife.Bind;

/**
 * Created by wangqi on 2017/11/28.
 */

public class ComplainResultFragment extends BaseLazyFragment implements HttpTaskListener {
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.state_layout)
    StateLayout stateLayout;

    private String outsourceFlag;
    private String newRequestId;
    private WorkHistoryPagerAdapter adapter;

    public ComplainResultFragment(String newRequestId, String outsourceFlag) {
        this.newRequestId = newRequestId;
        this.outsourceFlag = outsourceFlag;
    }

    public ComplainResultFragment() {
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_problem_process_pager;
    }

    @Override
    public void onLazyLoad() {
        initView();
        initData();
    }

    private void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new WorkHistoryPagerAdapter(getActivity());
        recyclerView.setAdapter(adapter);
    }

    private void initData() {
        LogUtil.e("投诉结果的id ="+newRequestId);
        HashMap<String, String> map = new HashMap<>();
        map.put("newRequestId", newRequestId);
        if ("1".equals(companyType)) {
            map.put("ownerCode", ownerBean.getRows().getOwnerCode());
        } else {
            map.put("serviceProviderCode", serviceLoginBean.getRows().getServiceProviderCode());
        }
        ServiceRequestManager.getManager().getComplainResult(getContext(),outsourceFlag, JsonMapUtil.mapToJson(map), this);
    }

    @Override
    public void onSuccess(int requestId, String result) {
        ComplainResultBean bean = new Gson().fromJson(result, ComplainResultBean.class);
        if (bean.getMessage().getCode() == 200) {
            adapter.addLastData(bean.getRows());
            if (bean.getRows() != null) {
                stateLayout.showContentView();
            }else{
                stateLayout.showEmptyView();
            }
        }

    }

    @Override
    public void onException(int requestId, String code) {
        stateLayout.showErrorView();
    }
}
